﻿Imports System.Globalization
Imports System.IO

Partial Public Class CN_DN_Report
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "CN\DN REPORT "
            txtDate.Text = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
            txtDateTo.Text = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
        End If
    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet

        Dim dt As New DataTable
        dt.Columns.Add("MAgent_name", GetType(String))
        dt.Columns.Add("Biiling_no", GetType(String))
        dt.Columns.Add("Text_line", GetType(String))
        dt.Columns.Add("DN_Qty", GetType(String))
        dt.Columns.Add("DN_Amount", GetType(String))
        dt.Columns.Add("DN_Sum", GetType(String))
        dt.Columns.Add("CN_Qty", GetType(String))
        dt.Columns.Add("CN_Amount", GetType(String))
        dt.Columns.Add("CN_Sum", GetType(String))
        dt.Columns.Add("Sale_Order", GetType(String))
        dt.Columns.Add("Total", GetType(String))
        dt.Columns.Add("Date_Current", GetType(String))
        dt.Columns.Add("Booking_no", GetType(String))
        dt.Columns.Add("ColorCN", GetType(String))
        dt.Columns.Add("ColorDN", GetType(String))
        dt.AcceptChanges()

        Dim dtCNDN As DataTable = clsGet.GetBlock_CNDN_Day(txtDate.Text.Split(" ")(2).Trim() & CDate(txtDate.Text).ToString("MMdd"), txtDateTo.Text.Split(" ")(2).Trim() & CDate(txtDateTo.Text).ToString("MMdd"))
        If dtCNDN.Rows.Count > 0 Then
            Dim SumDN As String = "0"
            Dim SumCN As String = "0"
            Dim Booking_no As String = dtCNDN.Rows(0).Item("Booking_no")
            Dim Sold_to As String = dtCNDN.Rows(0).Item("Sold_to")
            For Each dn As DataRow In dtCNDN.Rows
                If (Booking_no <> dn.Item("Booking_no")) Or (Sold_to <> dn.Item("Sold_to")) Then
                    SumDN = "0"
                    SumCN = "0"
                    Booking_no = dn.Item("Booking_no")
                    Sold_to = dn.Item("Sold_to")
                End If

                Dim dr As DataRow = dt.NewRow()
                Dim dtName As DataTable = clsGet.Get_Customer(dn.Item("Sold_to"))
                Dim Name As String
                If (dtName.Rows.Count > 0) Then
                    Name = dtName.Rows(0).Item("NAME1") & " " & dtName.Rows(0).Item("NAME2") & " " & dtName.Rows(0).Item("NAME3")
                Else
                    Name = dn.Item("MAgent_Name") & ""
                End If
                dr.Item("MAgent_name") = Name
                dr.Item("Biiling_no") = dn.Item("IM_REF_DOC") & ""
                dr.Item("Booking_no") = dn.Item("Booking_no") & ""
                Dim DN_QTY As String = ""
                Dim DN_Price As String = ""
                Dim CN_QTY As String = ""
                Dim CN_Price As String = ""
                If (dn.Item("IM_REASON") & "" = "Z03") Then
                    If (dn.Item("IM_DOCTYPE") = "ZCR") Then
                        CN_QTY = dn.Item("Target_Qty")
                        CN_Price = CDbl(dn.Item("Amount")) * CDbl(CN_QTY)
                        SumCN = (CDbl(SumCN) + CDbl(CN_Price)).ToString("#####################00.00")
                    Else
                        DN_QTY = dn.Item("Target_Qty")
                        DN_Price = CDbl(dn.Item("Amount")) * CDbl(DN_QTY)
                        SumDN = (CDbl(SumDN) + CDbl(DN_Price)).ToString("#####################00.00")
                    End If
                Else
                    If (dn.Item("Target_QU") & "" = "JOB") Then
                        If (dn.Item("IM_DOCTYPE") = "ZCR") Then
                            'Z02
                            ' CN_QTY = dn.Item("Target_Qty")
                            CN_QTY = ""
                            CN_Price = dn.Item("Amount")
                            SumCN = (CDbl(SumCN) + CDbl(CN_Price)).ToString("#####################00.00")
                        Else
                            'Z01 
                            ' DN_QTY = dn.Item("Target_Qty")
                            CN_QTY = ""
                            DN_Price = dn.Item("Amount")
                            SumDN = (CDbl(SumDN) + CDbl(DN_Price)).ToString("#####################00.00")
                        End If
                    Else
                        Dim dw As DataTable = clsGet.GetBookingTariff_Actual(dn.Item("Booking_no"), dn.Item("Material"), "")
                        If (dn.Item("IM_DOCTYPE") = "ZCR") Then
                            'Z01ใหม่น้อยกว่า
                            ' CN_QTY = dn.Item("Target_Qty")
                            CN_QTY = ""

                            If (dw.Rows.Count > 0) Then
                                CN_Price = ((CDbl(dn.Item("Amount") + CDbl(dw.Rows(0).Item("data2")))) * CDbl(dn.Item("Target_Qty"))) - (CDbl(dn.Item("Amount")) * CDbl(dn.Item("Target_Qty")))
                            Else
                                CN_Price = (CDbl(dn.Item("Amount")) * CDbl(dn.Item("Target_Qty")))
                            End If

                            SumCN = (CDbl(SumCN) + CDbl(CN_Price)).ToString("#####################00.00")
                        Else
                            'Z02  
                            ' DN_QTY = dn.Item("Target_Qty")
                            CN_QTY = ""
                            If (dw.Rows.Count > 0) Then
                                DN_Price = (CDbl(dn.Item("Amount")) * CDbl(dn.Item("Target_Qty"))) - ((CDbl(dn.Item("Amount") - CDbl(dw.Rows(0).Item("data2")))) * CDbl(dn.Item("Target_Qty")))
                            Else
                                DN_Price = (CDbl(dn.Item("Amount")) * CDbl(dn.Item("Target_Qty")))
                            End If
                            SumDN = (CDbl(SumDN) + CDbl(DN_Price)).ToString("#####################00.00")
                        End If
                    End If

                End If

                dr.Item("Text_line") = dn.Item("Text_line") & ""
                Try
                    dr.Item("DN_Qty") = FormatNumber(DN_QTY, 2)
                Catch ex As Exception
                    dr.Item("DN_Qty") = DN_QTY
                End Try
                Try
                    dr.Item("DN_Amount") = FormatNumber(DN_Price, 2)
                Catch ex As Exception
                    dr.Item("DN_Amount") = DN_Price
                End Try
                Try
                    dr.Item("CN_Qty") = FormatNumber(CN_QTY, 2)
                Catch ex As Exception
                    dr.Item("CN_Qty") = CN_QTY
                End Try
                Try
                    dr.Item("CN_Amount") = FormatNumber(CN_Price, 2)
                Catch ex As Exception
                    dr.Item("CN_Amount") = CN_Price
                End Try


                Dim dtTemp As DataTable = clsGet.Get_TempEstimateOneRecord(dn.Item("Booking_no"), dn.Item("MATERIAL"))
                Dim dtActual As DataTable = clsGet.Get_ActualOneRecord(dn.Item("Booking_no"), dn.Item("MATERIAL"))
                If (dtTemp.Rows.Count > dtActual.Rows.Count) Then
                    If (dr.Item("DN_Amount") <> "") Then
                        If (CDbl(dr.Item("DN_Amount")).ToString("###############0.00") <> CDbl(dtTemp.Rows(0).Item("data4")).ToString("###############0.00")) Then
                            SumDN = CDbl((CDbl(SumDN) - CDbl(dr.Item("DN_Amount"))) + CDbl(dtTemp.Rows(0).Item("data4"))).ToString("###############0.00")
                            dr.Item("DN_Amount") = FormatNumber(dtTemp.Rows(0).Item("data4"), 2)
                            dr.Item("ColorDN") = "DN"
                        End If
                    End If
                    If (dr.Item("CN_Amount") <> "") Then
                        If (CDbl(dr.Item("CN_Amount")).ToString("###############0.00") <> CDbl(dtTemp.Rows(0).Item("data4")).ToString("###############0.00")) Then
                            SumCN = CDbl((CDbl(SumCN) - CDbl(dr.Item("CN_Amount"))) + CDbl(dtTemp.Rows(0).Item("data4"))).ToString("###############0.00")
                            dr.Item("CN_Amount") = FormatNumber(dtTemp.Rows(0).Item("data4"), 2)
                            dr.Item("ColorCN") = "CN"
                        End If
                    End If
                ElseIf (dtTemp.Rows.Count = dtActual.Rows.Count) Then
                    If (dr.Item("DN_Amount") <> "") Then
                        If CDbl(CDbl(dtActual.Rows(0).Item("data4")) - CDbl(dr.Item("DN_Amount"))).ToString("###############0.00") <> CDbl(dtTemp.Rows(0).Item("data4")).ToString("###############0.00") Then
                            SumDN = (CDbl(SumDN) - CDbl(dr.Item("DN_Amount"))) + (CDbl(dtActual.Rows(0).Item("data4")) - CDbl(dtTemp.Rows(0).Item("data4")))
                            dr.Item("DN_Amount") = FormatNumber((CDbl(dtActual.Rows(0).Item("data4")) - CDbl(dtTemp.Rows(0).Item("data4"))).ToString("####################0.00"), 2)
                            dr.Item("ColorDN") = "DN"
                        End If
                    End If
                    If (dr.Item("CN_Amount") <> "") Then
                        If (CDbl(dtActual.Rows(0).Item("data4")) + CDbl(dr.Item("CN_Amount"))).ToString("###############0.00") <> CDbl(dtTemp.Rows(0).Item("data4")) Then
                            SumCN = (CDbl(SumCN) - CDbl(dr.Item("CN_Amount"))) + (CDbl(dtTemp.Rows(0).Item("data4")) - CDbl(dtActual.Rows(0).Item("data4")))
                            dr.Item("CN_Amount") = FormatNumber((CDbl(dtTemp.Rows(0).Item("data4")) - CDbl(dtActual.Rows(0).Item("data4"))).ToString("####################0.00"), 2)
                            dr.Item("ColorCN") = "CN"
                        End If
                    End If
                Else
                    If (dr.Item("DN_Amount") <> "") Then
                        If (CDbl(dr.Item("DN_Amount")).ToString("###############0.00") <> CDbl(dtActual.Rows(0).Item("data4")).ToString("###############0.00")) Then
                            SumDN = (CDbl(SumDN) - CDbl(dr.Item("DN_Amount"))) + CDbl(dtActual.Rows(0).Item("data4"))
                            dr.Item("DN_Amount") = FormatNumber(dtActual.Rows(0).Item("data4"), 2)
                            dr.Item("ColorDN") = "DN"
                        End If
                    End If
                    If (dr.Item("CN_Amount") <> "") Then
                        If (CDbl(dr.Item("CN_Amount")).ToString("###############0.00") <> CDbl(dtActual.Rows(0).Item("data4")).ToString("###############0.00")) Then
                            SumCN = (CDbl(SumCN) - CDbl(dr.Item("CN_Amount"))) + CDbl(dtActual.Rows(0).Item("data4"))
                            dr.Item("CN_Amount") = FormatNumber(dtActual.Rows(0).Item("data4"), 2)
                            dr.Item("ColorCN") = "CN"
                        End If
                    End If
                End If

                dr.Item("DN_Sum") = SumDN
                dr.Item("CN_Sum") = SumCN
                dr.Item("Sale_Order") = dn.Item("EX_SALES_NO") & ""
                dr.Item("Total") = (CDbl(SumDN) - CDbl(SumCN)).ToString("#########################0.00")
                dr.Item("Date_Current") = Date.Now.ToString("dd/MM/yyyy", New CultureInfo("en-US"))

                dt.Rows.Add(dr)
                dt.AcceptChanges()
            Next
            Booking_no = ""
            For Each dr As DataRow In dt.Rows
                If (dr.Item("Booking_no") <> Booking_no) Then
                    SumDN = "0"
                    SumCN = "0"
                    For Each dx As DataRow In dt.Rows
                        If (dr.Item("Booking_no") = dx.Item("Booking_no")) And (dr.Item("MAgent_name") = dx.Item("MAgent_name")) Then
                            Try
                                If (CDbl(SumDN) < CDbl(dx.Item("DN_SUM"))) Then
                                    SumDN = dx.Item("DN_SUM")
                                End If
                            Catch ex As Exception

                            End Try
                            Try
                                If (CDbl(SumCN) < CDbl(dx.Item("CN_SUM"))) Then
                                    SumCN = dx.Item("CN_SUM")
                                End If
                            Catch ex As Exception

                            End Try
                        End If
                    Next
                End If
                Try
                    dr.Item("DN_SUM") = FormatNumber(SumDN, 2)
                Catch ex As Exception
                    dr.Item("DN_SUM") = SumDN
                End Try
                Try
                    dr.Item("CN_SUM") = FormatNumber(SumCN, 2)
                Catch ex As Exception
                    dr.Item("CN_SUM") = SumCN
                End Try

                dr.Item("Total") = FormatNumber((CDbl(SumDN) - CDbl(SumCN)).ToString("#########################0.00"), 2)
                dt.AcceptChanges()
            Next
            dsReport.Tables.Add(dt)
            dsReport.AcceptChanges()
            ReportName = "CNDN.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "CNDN_CNDN"
            paraName = "Date_Current"
            paraValue = Date.Now.ToString("dd/MM/yyyy", New CultureInfo("en-US"))
            Orientation = True
            reportType = radioType.SelectedValue

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "CNDN_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบรายการ CN/DN');", True)
        End If
    End Sub
End Class